package urltest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class TestUrl {
	private static String getSession() throws Exception{
		
		URL url=new URL("http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
		HttpURLConnection con=null;           
		con=(HttpURLConnection)url.openConnection();
		con.setRequestMethod("GET");
		con.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		con.setRequestProperty("Connection", "keep-alive");
		con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
		con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
		con.setRequestProperty("Cache-Control", "max-age=0");
//		con.setRequestProperty("Content-Length", "49");
//		con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		con.setRequestProperty("Host", "1.85.55.152");
//		con.setRequestProperty("Origin", "http://1.85.55.152");
//		con.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
		con.setRequestProperty("Upgrade-Insecure-Requests", "1");
		con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
		con.setDoOutput(true);  
		con.setDoInput(true);   

		InputStream in=con.getInputStream();
		String session = con.getHeaderField("Set-Cookie");
		System.out.println(session);
		String[] sessionid = session.split(";");
		System.out.println(sessionid[0]);
		in.close();
		
		return sessionid[0];
	}
	
	public static void main(String[] args) throws Exception{
		
		//Request1
//		URL url=new URL("http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
//		HttpURLConnection con=null;           
//		con=(HttpURLConnection)url.openConnection();
//		con.setRequestMethod("GET");
//		con.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//		con.setRequestProperty("Connection", "keep-alive");
//		con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
//		con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
//		con.setRequestProperty("Cache-Control", "max-age=0");
////		con.setRequestProperty("Content-Length", "49");
////		con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//		con.setRequestProperty("Host", "1.85.55.152");
////		con.setRequestProperty("Origin", "http://1.85.55.152");
////		con.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
//		con.setRequestProperty("Upgrade-Insecure-Requests", "1");
//		con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
//		con.setDoOutput(true);  
//		con.setDoInput(true);   
//
//		InputStream in=con.getInputStream();
//		String session = con.getHeaderField("Set-Cookie");
//		System.out.println(session);
//		String[] sessionid = session.split(";");
//		System.out.println(sessionid[0]);
//		in.close();
		
		String sessionid = getSession();
		
				//Request2
		URL url2 = new URL("http://1.85.55.152/sys/ValidateCode.aspx");
		HttpURLConnection con2 = (HttpURLConnection)url2.openConnection();
		
		con2.setRequestMethod("GET");
		con2.setRequestProperty("Accept",  "image/webp,image/*,*/*;q=0.8");
		con2.setRequestProperty("Cookie", sessionid);
		System.out.println(sessionid+"hello");
		con2.setRequestProperty("Connection", "keep-alive");
		con2.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
		con2.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
//		con2.setRequestProperty("Cache-Control", "max-age=0");
//		con2.setRequestProperty("Content-Length", "49");
//		con2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		con2.setRequestProperty("Host", "1.85.55.152");
//		con2.setRequestProperty("Origin", "http://1.85.55.152");
		con2.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
//		con2.setRequestProperty("Upgrade-Insecure-Requests", "1");
		con2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
		con2.setDoInput(true);
		con2.setDoOutput(true);
		con2.connect();
		String session2 = con2.getHeaderField("Set-Cookie");
		System.out.println(sessionid);
		System.out.println("session2..."+session2);
		
		BufferedInputStream in2 = new BufferedInputStream(con2.getInputStream());
		BufferedOutputStream out2 = new BufferedOutputStream(new FileOutputStream("checkCode2.jpg"));
		byte[] buffer = new byte[128];
		int len = 0;
		while ((len = in2.read(buffer)) != -1) {
			out2.write(buffer, 0, len);
		}
		
		Map<String, List<String>> map = con2.getHeaderFields();
		for(String key:map.keySet()){
			for(String s:map.get(key)){
				System.out.println(key+":"+s);
			}				
		};
		
		out2.close();
		in2.close();
		
		System.out.println("Enter the validation code:");
		BufferedReader Sreader = new BufferedReader(new InputStreamReader(System.in));
//		byte[] buff = new byte[128];
//		int m = 0;
//		while ((m))
		String vc= Sreader.readLine();
		System.out.printf("%s", vc);

		
		//Request3
		URL url3 = new URL("http://1.85.55.152/ZNPK/TeacherKBFB_rpt.aspx");
		HttpURLConnection con3 = (HttpURLConnection)url3.openConnection();
		
		con3.setRequestMethod("POST");
		con3.setRequestProperty("Accept",  "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		con3.setRequestProperty("Cookie", sessionid);
		con3.setRequestProperty("Accept-Encoding", "gzip, deflate");
		con3.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
		con3.setRequestProperty("Cache-Control", "max-age=0");
		con3.setRequestProperty("Connection", "keep-alive");
		con3.setRequestProperty("Content-Length", "49");
		con3.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		con3.setRequestProperty("Host", "1.85.55.152");
		con3.setRequestProperty("Origin", "http://1.85.55.152");
		con3.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
		con3.setRequestProperty("Upgrade-Insecure-Requests", "1");
		con3.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
		con3.setDoInput(true);
		con3.setDoOutput(true);
		
//		BufferedInputStream in3 = new BufferedInputStream(con3.getInputStream());
		BufferedOutputStream out3 = new BufferedOutputStream(con3.getOutputStream());
		String reqStr = "Sel_XNXQ=20161&Sel_JS=0000155&type=1&txt_yzm="+vc;
		out3.write(reqStr.getBytes());
		InputStream in3 = con3.getInputStream();

		String resStr = "";
		BufferedReader reader3 = new BufferedReader(new InputStreamReader(in3));
		String line = "";
		while ((line = reader3.readLine()) != null) {
			resStr += line;
		}
		File html = new File("res");
		OutputStream outres = new FileOutputStream(html);
		outres.write(resStr.getBytes());
		
		String session3 = con3.getHeaderField("Set-Cookie");
		System.out.println(session3);
		out3.close();
		in3.close();		
	}
}